package com.ssm.controller;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.bean.Store;
import com.ssm.service.StoreService;

/**
 * @author ZhaoXianming
 * @Description 仓库管理
 * @date 2020年3月15日
 */
@Controller
@RequestMapping("/storeManagement")
public class StoreManagementController extends BaseController {

	Logger log = LoggerFactory.getLogger(StoreManagementController.class);
	
	@Autowired
	public StoreService storeService;
	
	@RequestMapping("/toStoreManagementJsp")
	public String toJsp(){
		return "storeManagement/storeList";
	}
	
	@RequestMapping("/getStores/{pageNo}/{pageSize}")
	@ResponseBody
	public Map<String,Object> getStores(@PathVariable int pageNo, @PathVariable int pageSize, 
			@RequestParam String name, String code){
		Map<String,Object> params = new HashMap<>();
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		try {
			params.put("storeName", new String(name.getBytes("ISO-8859-1"), "UTF-8"));
			params.put("storeCode", code);
			int cnt = storeService.getStoresCnt(params);
			if(cnt > 0){
				params.put("totalPage", (int)Math.ceil((double)cnt/10));
				params.put("totalRecord", cnt);
				List<Store> list = storeService.getStores(params);
				params.put("storeList", list);
				return params;
			}
		} catch (UnsupportedEncodingException e) {
			log.error("查询出错了", e);
		}
		return Collections.emptyMap();
	}
	
	@RequestMapping("/toAddStoreJsp")
	public String toAddJsp(){
		return "storeManagement/storeAdd";
	}
	
	@RequestMapping("/toUpdateStoreJsp")
	public String toUpdateJsp(HttpServletRequest request, Model model, String storeCode){
		Store store = storeService.getStoreByCode(storeCode);
		model.addAttribute("store",store);
		return "storeManagement/storeUpdate";
	}
	
	@RequestMapping("/isStoreRepeated")
	@ResponseBody
	public boolean isStoreRepeated(String storeCode){
		int row = storeService.isRepeated(storeCode);
		return row > 0?false:true;
	}
	
	@RequestMapping("/addOrUpdateStore")
	@ResponseBody
	public String addOrUpdate(@RequestParam String storeName, String storeCode){
		try{
			storeName = new String(storeName.getBytes("ISO-8859-1"), "UTF-8");
			storeService.saveOrUpdateStore(storeName, storeCode);
			return "success";
		}catch(Exception e){
			log.error("添加/更新仓库出错了", e);
			return "error";
		}
	}
	
	@RequestMapping("/deleteStores")
	@ResponseBody
	public void deleteDevices(@RequestParam List<String> storeCodes){
		storeService.deleteStores(storeCodes);
	}
}
